package com.wyhello.tube4.spring.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.pojo.Bag;
import com.wyhello.tube4.pojo.Position;
import com.wyhello.tube4.pojo.Reality;
import com.wyhello.tube4.pojo.Reside;
import com.wyhello.tube4.pojo.Tube;
import com.wyhello.tube4.spring.model.BagRecordsModel;
import com.wyhello.tube4.spring.model.ResideRecordsModel;
import com.wyhello.tube4.spring.model.WarningRecordsModel;
@Controller
public class WarningRecordsController {
	@Autowired
	private WarningRecordsModel warnRecordsModel;
	
	@Autowired
	private ResideRecordsModel resideRecordsModel;
	
	@Autowired
	private BagRecordsModel bagRecordsModel;
	
	@RequestMapping("/warning/queryWarning")
	public ModelAndView queryWarning(){
		List<Reality> realitys = warnRecordsModel.queryRealityWarn();
		List<Reality> warnList = new ArrayList<Reality>();
		for (Reality reality : realitys) {
			if(reality.getOuterDiameterX() >= reality.getPosition().getOutWarnValue()||
					reality.getOuterDiameterY() >= reality.getPosition().getOutWarnValue()||
					reality.getCreepValueX() >= reality.getPosition().getCreepWarnValue()||
					reality.getCreepValueY() >= reality.getPosition().getCreepWarnValue()||
					reality.getWearRate() >= reality.getPosition().getSpeedValue()||
					reality.getLifetime() <= reality.getPosition().getLifeValue())
			{
				warnList.add(reality);
			}
		}
		ModelAndView model = new ModelAndView("overproof");
		model.addObject("warnList", warnList);
		return model;
	}
	
	/*@RequestMapping("/warning/queryYearWarning")
	public ModelAndView queryYearWarning(){
		long date = new Date().getTime() - Long.valueOf("31557600000");
		
		List<Reality> realitys = warnRecordsModel.queryRealityWarn();
		List<Reality> yearList = new ArrayList<Reality>();
		for (Reality reality : realitys) {
			if(reality.getTime().getTime() <= date){
				yearList.add(reality);
			}
		}
		ModelAndView model = new ModelAndView("pass");
		model.addObject("yearList", yearList);
		return model;
	}*/
	
	/**
	 * 
	 * @param count 显示多少条记录，0为全部显示
	 * @return
	 */
	/*@RequestMapping("/warning/queryYearWarning")
	public ModelAndView queryYearWarning(int count){
		
		Calendar date = new GregorianCalendar();
		int lastyear = date.get(Calendar.YEAR) - 1;
		date.set(Calendar.YEAR, lastyear);
		//System.out.println(date);
		Date date = new Date();
		int lastyear = date.getYear() - 1;
		date.setYear(lastyear);
		
		List<Reside> boilers =  resideRecordsModel.queryResideByBoiler();
		Iterator<Reside> itBoilers = boilers.iterator();
		List<Map<String, List<Position>>> boilerslist = new ArrayList<Map<String, List<Position>>>();
		//锅炉
		while(itBoilers.hasNext()){
			Reside boiler = itBoilers.next();
			Map map = new HashMap();
			List<Position> positionList = new ArrayList<Position>();
			
			//受热面
			Set<Reside> surfaces = boiler.getResides();
			Iterator<Reside> itSurfaces = surfaces.iterator();
			while(itSurfaces.hasNext()){
				Reside surface = itSurfaces.next();
				
				//管排
				Set<Reside> rows = surface.getResides();
				Iterator<Reside> itRows = rows.iterator();
				while(itRows.hasNext()){
					Reside row = itRows.next();
					
					//管
					Set<Tube> tubes = row.getTubes();
					Iterator<Tube> itTubes = tubes.iterator();
					while(itTubes.hasNext()){
						Tube tube = itTubes.next();
						
						//位置
						Set<Position> positions = tube.getPositions();
						Iterator<Position> itPositions = positions.iterator();
						while(itPositions.hasNext()){
							Position position = itPositions.next();
							if(count != 0 && positionList.size() >= count){
								break;
							}
							
							if(position.getRealitys() == null &&  position.getRealitys().size() == 0){
								//从未测量
								positionList.add(position);
							}else{
								//近一年内未测量	
								boolean warn = true;
								Set<Reality> realitys = position.getRealitys();
								Iterator<Reality> itRealitys = realitys.iterator();
								while(itRealitys.hasNext()){
									Reality reality = itRealitys.next();
									if(reality.getTime() != null && date.before(reality.getTime())){
										warn = false;
									}
								}
								
								if(warn){
									positionList.add(position);
								}
							}
							
						}
						
					}
				}
			}
			
			map.put("boilerName", boiler.getName());
			map.put("positionList", positionList);
			
			boilerslist.add(map);
		}
		
		ModelAndView model = new ModelAndView("pass","boilers",boilerslist);
	
		return model;
	}*/
	
	@RequestMapping("/warning/queryYearWarning")
	public ModelAndView queryYearWarning(int count){
		
		List<Reside> boilers =  resideRecordsModel.queryResideByBoiler();
		Iterator<Reside> itBoilers = boilers.iterator();
		List<Map<String, List<Position>>> boilerslist = new ArrayList<Map<String, List<Position>>>();
		//锅炉
		while(itBoilers.hasNext()){
			Reside boiler = itBoilers.next();
			Map map = new HashMap();
			
			Map<Integer,List<Position>> positionListInfo = warnRecordsModel.getPositionsNotCheckedThisYear(boiler.getId(), count);
			map.put("boilerName", boiler.getName());
			map.put("positionList", positionListInfo.get("positions"));
			map.put("count", positionListInfo.get("count"));
			boilerslist.add(map);
		}
		
		ModelAndView model = new ModelAndView("pass");
		model.addObject("boilers", boilerslist);
		
		return model;
	}
	
	/*@RequestMapping("/warning/queryLast")
	public ModelAndView queryLast(){
		ModelAndView model = new ModelAndView("last");
		List<Reside> resides = resideRecordsModel.queryResideByBoiler();
		List<Position> listPo = new ArrayList<Position>();
		List<Position> positions = warnRecordsModel.queryPosition();
		List<Integer> list = new ArrayList<Integer>();
		for (Reside reside : resides) {
			List<Bag> bags = bagRecordsModel.queryBag(reside.getId());
			if(bags.size() != 0){
				Bag bag = bags.get(0);
				for (Reality reality : bag.getRealitys()) {
					list.add(reality.getPosition().getId());
				}
			}
		}
		for (Position position : positions) {
			if(!list.contains(position.getId())){
				listPo.add(position);
			}
		}
		model.addObject("listPo", listPo);
		return model;
	}*/
	
	@RequestMapping("/warning/queryLast")
	public ModelAndView queryLast(int count){
		List<Reside> boilers =  resideRecordsModel.queryResideByBoiler();
		Iterator<Reside> itBoilers = boilers.iterator();
		List<Map<String, List<Position>>> boilerslist = new ArrayList<Map<String, List<Position>>>();
		//锅炉
		while(itBoilers.hasNext()){
			Reside boiler = itBoilers.next();
			Map map = new HashMap();
			
			Map<Integer,List<Position>> positionListInfo = warnRecordsModel.getPositionsNotCheckLasttime(boiler.getId(), count);
			map.put("boilerName", boiler.getName());
			map.put("positionList", positionListInfo.get("positions"));
			map.put("count", positionListInfo.get("count"));
			boilerslist.add(map);
		}
		
		ModelAndView model = new ModelAndView("last");
		model.addObject("boilers", boilerslist);
		
		return model;
	}
	
	/*@RequestMapping("/warning/queryNoReality")
	public ModelAndView queryNoReality(){
		
		List<Position> positions = warnRecordsModel.queryPosition();
		List<Position> poNoReal = new ArrayList<Position>();
		for (Position position : positions) {
			if(position.getRealitys().size() == 0){
				poNoReal.add(position);
			}
		}
		ModelAndView model = new ModelAndView("never");
		model.addObject("poNoReal", poNoReal);
		return model;
	}*/
	
	
	@RequestMapping("/warning/queryNoReality")
	public ModelAndView queryNoReality(int count){
		
		List<Reside> boilers =  resideRecordsModel.queryResideByBoiler();
		Iterator<Reside> itBoilers = boilers.iterator();
		List<Map<String, List<Position>>> boilerslist = new ArrayList<Map<String, List<Position>>>();
		//锅炉
		while(itBoilers.hasNext()){
			Reside boiler = itBoilers.next();
			Map map = new HashMap();
			
			Map<Integer,List<Position>> positionListInfo = warnRecordsModel.getPositionNeverChecked(boiler.getId(), count);
			map.put("boilerName", boiler.getName());
			map.put("positionList", positionListInfo.get("positions"));
			map.put("count", positionListInfo.get("count"));
			boilerslist.add(map);
		}
		
		ModelAndView model = new ModelAndView("never");
		model.addObject("boilers", boilerslist);
		return model;
	}
	
	
}
